<template>
  <div>
    <h1>英雄列表</h1>
    <div>
      <el-table :data="tableData">
        <el-table-column prop="_id" label="ID" width="260">
        </el-table-column>
        <el-table-column prop="name" label="英雄名称" width="120">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="160">
          <template slot-scope="scope">
            <el-button type="primary" @click="$router.push(`/heros/edit/${scope.row._id}`)" size="small">编辑</el-button>
            <el-button type="danger" @click="remove(scope.row)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
export default {
  name: 'HeroList',
  data () {
    return {
      tableData: []
    }
  },
  created () {
    this.fetch()
  },
  methods: {
    async fetch () {
      const result = await this.$http.get('/rest/heros')
      if(result.data){
         this.tableData = result.data
      }
    },
    async remove (row) {
      this.$confirm(`是否要删除英雄"${row.name}"?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const result = await this.$http.delete(`/rest/heros /${row._id}`)
        if(result.data.success){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetch()
        }else{
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        }
      }).catch(()=>{
        
      })
    }
  }
}
</script>
